package com.etc.controller;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.etc.dto.Result;
import com.etc.entity.Business;
import com.etc.entity.Customer;
import com.etc.entity.PageInfo;
import com.etc.service.AdminService;
import com.etc.service.impl.AdminServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class BusinessServlet
 */
@WebServlet("/BusinessServlet")
public class BusinessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminService adminService = new AdminServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BusinessServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op = request.getParameter("op");
		
		if("findBusinessByPage".equals(op)) {
			doGetBusinessListByPage(request,response);
		}else if("deleteBusinessById".equals(op)) {
			deleteBusinessById(request,response);
		}else if("addBusiness".equals(op)) {
			addBusiness(request,response);
		}else if("updateBusiness".equals(op)) {
			updateBusiness(request,response);
		}else if("findById".equals(op)) {
			doGetBusinessById(request,response);
		}else if ("updateStatus".equals(op)) {
			doUpDateBusinessStatus(request,response);
		}
	}
	
	private void doUpDateBusinessStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		// 响应统一格式对象
		Result<Boolean> result = new Result<Boolean>();

		// 接收参数
		String businessId = request.getParameter("businessId");
		String status = request.getParameter("status");

		// 对参数进行效验
		if (businessId == null || status == null) {
			result.fail(0, "参数错误");
		} else {
			// 调用业务,更新状态
			boolean flag = adminService.updateBusinessStatus(Integer.parseInt(status), Integer.parseInt(businessId));
			if (flag) {
				result.ok(flag);
			} else {
				result.fail(0, "更新状态失败");
			}
		}

		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);

	}

	private void doGetBusinessById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		Result<Business> result = new Result<Business>();
		//接受请求参数
		String businessId = request.getParameter("businessId");
		//对参数进行校验中
		if(businessId == null) {
			result.fail(0, "参数错误");
		}else {
			//
			Business business = adminService.getBusinessById(Integer.parseInt(businessId));
			result.ok(business);
		}
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
	}

	private void updateBusiness(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// 响应统一格式对象
		Result<Boolean> result = new Result<Boolean>();
		
		// 接收参数
		String businessId = request.getParameter("businessId");
		String nickname = request.getParameter("nickname"); // 修改必须获取商品编号
		String mobile = request.getParameter("mobile");
		String password = request.getParameter("password");
		String status = request.getParameter("status"); // on
		
		// 构建用户对象
		Business  business = new Business();
		business.setBusinessId(Integer.parseInt(businessId));
		business.setNickname(nickname);
		business.setMobile(mobile);
		business.setPassword(password);
		business.setStatus(status == null ? 0 : 1);

		Boolean flag = adminService.updateBusiness(business);
	
		if (flag) {
			// 修改成功
			result.ok(flag);
		} else {
			// 修改失败
			result.fail(0, "修改商家出错");
		}
		
		
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
	
}



	private void addBusiness(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// 响应统一格式对象
		Result<Boolean> result = new Result<Boolean>();
		// 接收参数
		String nickname = request.getParameter("nickname"); 
		String mobile = request.getParameter("mobile");
		String password = request.getParameter("password");
		String status = request.getParameter("status"); // on
		Boolean flag2 = true;
	
		List<Business> list = adminService.getBusinesssList();
		for (Business business : list) {
			if(nickname.equals(business.getNickname())) {
				result.fail(0, "商家昵称已存在");
				flag2 = false;
				response.setContentType("application/json;charset=utf-8");
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				response.getWriter().write(json);
				
			}else if(mobile.equals(business.getMobile())) {
				result.fail(0, "商家手机号已存在");
				flag2 = false;
				response.setContentType("application/json;charset=utf-8");
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				response.getWriter().write(json);
			}
		}
			
			
			if(flag2){
				// 构建商家对象
				Business business = new Business();
				business.setNickname(nickname);
				business.setMobile(mobile);
				business.setPassword(password);
				business.setStatus(status == null ? 0 : 1);
							
				Boolean flag = adminService.addBusiness(business);

				if (flag) {
					// 修改成功
					result.ok(flag);
				} else {
					// 修改失败
					result.fail(0, "添加商家出错");
				}
				response.setContentType("application/json;charset=utf-8");
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				response.getWriter().write(json);
			
			}
		
	
}

	private void deleteBusinessById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//响应统一格式对象
		Result result = new Result();
		//接受请求参数
		String businessId = request.getParameter("businessId");
		
		//对参数进行校验
		if(businessId == null) {
			result.fail(0, "未获得参数");
		}else {
			//调用业务层
			Boolean flag = adminService.deleteBusinessById(Integer.parseInt(businessId));
			result.ok(flag);
		}
		//响应
		response.setContentType("appliction/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
	}

	private void doGetBusinessListByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//响应统一格式对象
		Result result = new Result();
		
		//接受请求参数
		String nickNameKeyWord = request.getParameter("nickNameKeyWord");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		
		//对参数进行校验
		if(pageNum==null || pageSize==null) {
			result.fail(0, "参数错误");
		}else {
			//调用业务层
			PageInfo<Business> pageInfo = adminService.getBusinessListByPage(nickNameKeyWord, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
			result.ok(pageInfo);
		}
		
		//响应
		response.setContentType("appliction/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
	}
	

}
